package android.vn;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity 
{
	private ImageView selectedImageView;
	private ImageView leftArrowImageView;
	private ImageView rightArrowImageView;
	private Gallery gallery;
	private int selectedImagePosition = 0;
	private List<Drawable> drawables;
	private ImageAdapterGallery galImageAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//Lấy danh sách hình ảnh
		getDrawablesList();
		//Thực hiện thiết lập giao diện
		setupUI();
	}

	//Thiết lập các biến sử dụng từ giao diện xml
	private void setupUI() 
	{
		selectedImageView = (ImageView) findViewById(R.id.selected_imageview);
		leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
		rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);
		gallery = (Gallery) findViewById(R.id.gallery);

		//Tạo 1 lắng nge từ nút bên trái
		leftArrowImageView.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				if (selectedImagePosition > 0)
				{
					--selectedImagePosition;
				}
				gallery.setSelection(selectedImagePosition, false);
			}
		});

		//Tạo 1 lắng nge từ nút bên phải
		rightArrowImageView.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				if (selectedImagePosition < drawables.size() - 1) 
				{
					++selectedImagePosition;
				}
				gallery.setSelection(selectedImagePosition, false);
			}
		});

		//Tạo lắng nge từ đối tượng Gallery		
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() 
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
			{
				selectedImagePosition = pos;
				//Kiểm tra vị trí ảnh trong danh sách hình ảnh để thiết lập 2 nút mũi tên như mong muốn
				if (selectedImagePosition > 0 && selectedImagePosition < drawables.size() - 1) 
				{
					leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
					rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
				} 
				else if (selectedImagePosition == 0) 
				{
					leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
				} 
				else if (selectedImagePosition == drawables.size() - 1) 
				{
					rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
				}
				//Thực hiện cập nhật lại viền
				changeBorderForSelectedImage(selectedImagePosition);
				//Cập nhật hình ảnh đc lựa chọn
				setSelectedImage(selectedImagePosition);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) 
			{

			}

		});

		//Tạo đối tượng ImageAdapterGallery
		galImageAdapter = new ImageAdapterGallery(this, drawables);
		//Thiết lập nguồn và đổ vào gallery
		gallery.setAdapter(galImageAdapter);
		if (drawables.size() > 0) 
		{
			gallery.setSelection(selectedImagePosition, false);
		}

		if (drawables.size() == 1) 
		{
			rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
		}
	}

	//Thực hiện thay đổi viền cho hình ảnh được lựa chọn
	private void changeBorderForSelectedImage(int selectedItemPos) 
	{
		int count = gallery.getChildCount();
		for (int i = 0; i < count; i++) 
		{
			ImageView imageView = (ImageView) gallery.getChildAt(i);
			imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_border));
			imageView.setPadding(3, 3, 3, 3);
		}

		ImageView imageView = (ImageView) gallery.getSelectedView();
		imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.selected_image_border));
		imageView.setPadding(3, 3, 3, 3);
	}

	//Lấy danh sách các hình ảnh đưa vào mảng để quản lý
	private void getDrawablesList() 
	{
		drawables = new ArrayList<Drawable>();
		drawables.add(getResources().getDrawable(R.drawable.natureimage1));
		drawables.add(getResources().getDrawable(R.drawable.natureimage2));
		drawables.add(getResources().getDrawable(R.drawable.natureimage3));
		drawables.add(getResources().getDrawable(R.drawable.natureimage4));
		drawables.add(getResources().getDrawable(R.drawable.natureimage5));
		drawables.add(getResources().getDrawable(R.drawable.natureimage6));
		drawables.add(getResources().getDrawable(R.drawable.natureimage7));
		drawables.add(getResources().getDrawable(R.drawable.natureimage8));
		drawables.add(getResources().getDrawable(R.drawable.natureimage9));
		drawables.add(getResources().getDrawable(R.drawable.natureimage10));
		drawables.add(getResources().getDrawable(R.drawable.natureimage11));
		drawables.add(getResources().getDrawable(R.drawable.natureimage12));
		drawables.add(getResources().getDrawable(R.drawable.natureimage13));
		drawables.add(getResources().getDrawable(R.drawable.natureimage14));
		drawables.add(getResources().getDrawable(R.drawable.natureimage15));
	}

	//Thiết lập hình ảnh lựa chọn hiển thị trên ImageView chính
	private void setSelectedImage(int selectedImagePosition) 
	{
		BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
		Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
		selectedImageView.setImageBitmap(b);
		selectedImageView.setScaleType(ScaleType.FIT_XY);
	}
}
